﻿
/// <reference path="vswd-ext_2.2.js" />
/********************poi.js*********************************
*Create Date: 2011-6
*Description : this js file use ext js to create AJAX RFID function window 
*********************************************************************/
function ShowEditOrAddRFIDWindow(editData, store,isShowInfo) {
    var isEdit = editData != null ? true : false; //如果为真就显示编辑窗口，否则就显示添加窗口
    var RFIDName = new Ext.form.TextField({
        allowBlank: true,
        maxLength: 32,
        emptyText: "",
        maxText: _Message_MaxText + ' 32'
    });
    var isReadOnly = isShowInfo == true ? true : false;
    var driverfields = ["driver_id", "driver_name", "driver_age", "driver_email", "driver_phone", "driver_cellphone", "driver_license_model1", "driver_license_model2", "driver_license_model3", "driver_creator_type", "driver_account", "driver_remarks"];
    var driverstore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: "HandlerSearchDriver",
            method: "POST"
        }),
        reader: new Ext.data.JsonReader({
            fields: driverfields,
            root: "data",
            totalProperty: "totalCount"
        }),
        autoLoad: true
    });

    var driverCombo = new Ext.form.ComboBox({
        name: 'driver_id',
        hiddenName: 'driver_id',
        typeAhead: true,
        triggerAction: 'all',
        lazyRender: true,
        mode: 'local',
        store: driverstore,
        valueField: 'driver_id',
        displayField: 'driver_name',
        editable: false,
        readOnly:isReadOnly,
        fieldLabel: _DriverResource_Driver
    });

    var form = null;

    var mybuttons = new Array();

    if (isEdit) {
        form = new Ext.FormPanel({
            width: 375,
            autoHeight: true,
            plain: true,
            layout: "form",
            defaultType: "textfield",
            labelWidth: 100,
            plain: true,
            baseCls: "x-plain",
            defaults: { anchor: "95%", msgTarget: "side" },
            buttonAlign: "center",
            bodyStyle: "padding:0 0 0 0",
            items: [{
                name: "RFID",
                fieldLabel: _RFIDResource_RFIDNumerStart,
                maxLength: 255,
                maxText: '255' + _Message_MaxText,
                allowBlank: false,
                blankText: _Message_BlankText,
                readOnly: isReadOnly,
                regex: /^[0-9.]{5,32}$/
            }, driverCombo
]
        });
        mybuttons.push({
            text: _CustomerConfigResource_Update,
            minWidth: 80,
            handler: function() {
                if (form.getForm().isValid()) {
                    Ext.MessageBox.show({
                        msg: _CustomerConfigResource_Saving + "...",
                        progressText: _CustomerConfigResource_Saving + "...",
                        width: 300,
                        wait: true,
                        waitConfig: { interval: 200 },
                        icon: 'download',
                        animEl: 'saving'
                    });
                    form.getForm().submit({
                        url: "HandlerADURFID",
                        method: "POST",
                        success: function(form, action) {
                            var flag = action.result.success;
                            if (flag == true) {
                                Ext.MessageBox.hide();
                                msgUpdateSuccess();
                                win.hide();
                                store.reload({
                                    params: {
                                        namekey: ''
                                    }
                                });
                            }
                        },
                        failure: function(form, action) {
                            msgUpdateFailure();
                        },
                        params: {
                            RFID_auto_id: editData.data.RFID_auto_id,
                            controlType: 'update'
                        }
                    });
                }
            }
        });
    }
    else {
        form = new Ext.FormPanel({
            width: 375,
            autoHeight: true,
            plain: true,
            layout: "form",
            defaultType: "textfield",
            labelWidth: 100,
            plain: true,
            baseCls: "x-plain",
            defaults: { anchor: "95%", msgTarget: "side" },
            buttonAlign: "center",
            bodyStyle: "padding:0 0 0 0",
            items: [{
                name: "RFID",
                fieldLabel: _RFIDResource_RFIDNumerStart,
                maxLength: 255,
                maxText: '255' + _Message_MaxText,
                allowBlank: false,
                blankText: _Message_BlankText,
                readOnly: isReadOnly,
                regex: /^[0-9.]{5,32}$/
            }, {
                name: "RFIDEnd",
                fieldLabel: _RFIDResource_RFIDNumerEnd,
                maxLength: 255,
                maxText: '255' + _Message_MaxText,
                allowBlank: false,
                blankText: _Message_BlankText,
                readOnly: isReadOnly,
                regex: /^[0-9.]{5,32}$/
            }, driverCombo
]
        });
        mybuttons.push({
            text: _NewForText_Confirm,
            handler: function() {
                if (form.getForm().isValid()) {
                    form.getForm().submit({
                        url: "HandlerADURFID",
                        method: "POST",
                        success: function(form, action) {
                            var flag = action.result.success;
                            if (flag == true) {
                                Ext.MessageBox.hide();
                                msgUpdateSuccess();
                                win.hide();
                                store.reload({
                                    params: {
                                        namekey: ''
                                    }
                                });
                            }
                        },
                        failure: function(form, action) {
                            msgUpdateFailure();
                        },
                        params: {
                            controlType: 'add'
                        }
                    });
                }
            }
        });
    }
    mybuttons.push({
        text: _NewForText_Reset,
        handler: function() {
            if (isEdit) {
                form.getForm().loadRecord(editData);
            }
            else {
                form.getForm().reset();
            }
        }
    });
    mybuttons.push({
        text: _NewForText_Cancel,
        minWidth: 80,
        handler: function() {
            win.close();
            win.destroy();
        }
    });
    if (isReadOnly)
        mybuttons = [];
    var win = new Ext.Window({
        width: 410,
        autoHeight: true,
        iconCls: "editicon",
        resizable: false,
        defaultType: "textfield",
        labelWidth: 100,
        collapsible: true,
        closable: true,
        plain: true,
        modal: 'true',
        buttonAlign: "center",
        bodyStyle: "padding:8px 0 0 10px",
        items: [form],
        listeners: {
            "show": function() {
                if (editData) {
                    form.getForm().loadRecord(editData);
                    driverCombo.setRawValue(editData.data.driver_name);

                }
            }
        },
        buttons: mybuttons
    });
    win.show();
}

function RFIDWindow() {
    var RFIDfields = ["RFID_auto_id", "RFID", "driver_id", "driver_name", "driver_age", "driver_email", "driver_phone", "driver_cellphone", "driver_license_model1", "driver_license_model2", "driver_license_model3", "driver_creator_type", "driver_account", "driver_remarks"];
    var RFIDstore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: "HandlerSearchRFID",
            method: "POST"
        }),
        reader: new Ext.data.JsonReader({
            fields: RFIDfields,
            root: "data",
            totalProperty: "totalCount"
        }),
        autoLoad: true,
        baseParams: { limit: 32767 }
    });

    var txtKey = new Ext.form.TextField({
        allowBlank: true,
        maxLength: 32,
        emptyText: "",
        maxText: _Message_MaxText + ' 32'
    });

    var tbar = new Ext.Toolbar({
        items: [
            txtKey, {
                tooltip: { text: _POI_Search, autoHide: true },
                icon: 'images/search16.gif',
                handler: function() {
                    RFIDstore.reload({
                        params: {
                            namekey: txtKey.getValue(),
                            start:0,
                            limit:2000
                        }
                    });
                }
            }, {
                xtype: 'tbseparator', width: 5
            }, {
                tooltip: { text: _RFIDResource_AddNewRFID, autoHide: true },
                icon: 'images/add.png',
                handler: function() {
                    ShowEditOrAddRFIDWindow(null, RFIDstore);
                }
            }, {
                tooltip: { text: _RFIDResource_EditRFID, autoHide: true },
                icon: 'images/edit.gif',
                handler: function() {
                    if (grid.getSelectionModel().getCount() == 1) {
                        ShowEditOrAddRFIDWindow(grid.getSelectionModel().getSelections()[0], RFIDstore);
                    }
                    else {
                        Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
                        Ext.Msg.show({
                            title: _TrackerMain_Title,
                            msg: _POI_SelectOne,
                            buttons: Ext.Msg.OK,
                            animEl: 'elId',
                            icon: Ext.MessageBox.INFO
                        });
                    }
                }
            }, {
                tooltip: { text: _RFIDResource_DeleteRFID, autoHide: true },
                icon: 'images/delete.png',
                handler: function() {
                    if (grid.getSelectionModel().getCount() == 1) {
                        Ext.MessageBox.YESNO = { yes: _TrackerMain_Yes, no: _TrackerMain_No };
                        Ext.MessageBox.confirm(_TrackerMain_Title, _RFIDResource_DeleteRFID, function(btn) {
                            if (btn == "yes") {
                                Ext.Ajax.request({
                                    url: 'HandlerADURFID',
                                    success: function(a, action) {
                                        var flag = Ext.util.JSON.decode(a.responseText).success;
                                        if (flag == true) {
                                            Ext.MessageBox.hide();
                                            msgUpdateSuccess();
                                            RFIDstore.reload({
                                                params: {
                                                    namekey: ''
                                                }
                                            });
                                        }
                                    },
                                    failure: function() {
                                        msgUpdateFailure();
                                    },
                                    params: {
                                        RFID_auto_id: grid.getSelectionModel().getSelections()[0].data.RFID_auto_id,
                                        controlType: 'delete'
                                    }
                                });
                            }
                        }, this);
                    }
                    else {
                        Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
                        Ext.Msg.show({
                            title: _TrackerMain_Title,
                            msg: _POI_SelectAtLeastOne,
                            buttons: Ext.Msg.OK,
                            animEl: 'elId',
                            icon: Ext.MessageBox.INFO
                        });
                    }
                }
            }
        ]
    });

    var grid = new Ext.grid.GridPanel({
        store: RFIDstore,
        columns: [
                { header: _RFIDResource_RFIDNumer, width: 200, sortable: true, menuDisabled: true, dataIndex: 'RFID' },
                { header: _DriverResource_Driver, width: 120, sortable: true, menuDisabled: true, dataIndex: 'driver_name' }
            ],
        width: 330,
        height: 200
    });

    grid.addListener('rowdblclick', function(obj, index) {
//        var record = grid.getStore().getAt(index);
//        ShowEditOrAddRFIDWindow(record, null, true);
    });

    var RFIDwindow = new Ext.Window({
        title: _RFIDResource_Title,
        layout: 'fit',
        height: 250,
        width: 350,
        items: grid,
        tbar: tbar,
        constrain:true,
        border: true,
        minimizable: true,
         listeners: {
            'minimize': function() {
                addTaskButtonForWindow(RFIDwindow);
            }
        }
    });

    RFIDwindow.show();
}